﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraSplashScreen;
using DevExpress.XtraTabbedMdi;
using WarehouseManagement.BusinessLogic.BC;
using WarehouseManagement.Shared;
using WarehouseManagement.Views.Currency;
using WarehouseManagement.Views.Customer;
using WarehouseManagement.Views.Export;
using WarehouseManagement.Views.Import;
using WarehouseManagement.Views.Login;
using WarehouseManagement.Views.Merchandise;
using WarehouseManagement.Views.PO;
using WarehouseManagement.Views.Stock;
using WarehouseManagement.Views.Supplier;


namespace WarehouseManagement.Views.Shared
{
    public partial class MainForm : RibbonForm
    {
        public static NewEditImport FrmImportNew;
        public static NewEditExport FrmNewEditExport;
        public static NewEditPO FrmNewEditPO;
        private static ViewPO FrmViewPO;
        private static ViewReceipts FrmViewReceipts;
        private static UpdateSupplierDebt FrmUpdateSupplierDebt;

        private StockByQuantityAndValue frmStockByQtyAndValue;
        private InputMerchandiseInStock FrmInputMerchaniseInStock;

        private ViewImport frmViewImport;
        private ViewExport frmViewExport;
        private ViewSupplierDebt frmViewSupplierDebt;
        private ViewRevenue FrmViewRevenue;

        private MerchandiseCategoryList FrmMerchandiseCategoryList;
        private MerchandiseList FrmMerchandiseList;

        private SupplierList FrmSupplierList;
        private CustomerList FrmCustomerList;
        private CurrencyList FrmCurrencyList;

        public delegate void FormClosedDelegate();
        public FormClosedDelegate OnFormClosed;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // Create a BarManager that will display a bar of commands at the top of the main form.
            //BarManager barManager = new BarManager();
            //barManager.Form = this;

            //// Create a bar with a New button.
            //barManager.BeginUpdate();
            //Bar bar = new Bar(barManager, "My Bar");
            //bar.DockStyle = BarDockStyle.Top;
            //barManager.MainMenu = bar;
            //BarItem barItem = new BarButtonItem(barManager, "New");
            //barItem.ItemClick += new ItemClickEventHandler(barItem_ItemClick);
            //bar.ItemLinks.Add(barItem);

            //barManager.EndUpdate();
            // Create an XtraTabbedMdiManager that will manage MDI child windows.

            SetupSystemInfo();
            XtraTabbedMdiManager mdiManager = new XtraTabbedMdiManager();
            mdiManager.MdiParent = this;
            mdiManager.ClosePageButtonShowMode = DevExpress.XtraTab.ClosePageButtonShowMode.InActiveTabPageHeaderAndOnMouseHover;

        }

        private void SetupSystemInfo()
        {
            lblUser.Caption = "Người dùng: " + GlobalMemory.CurrentLoginUser;
            lblCurrentTime.Caption = string.Format("Ngày {0} tháng {1} năm {2}", DateTime.Today.Day, DateTime.Today.Month, DateTime.Today.Year);
            lblVersion.Caption = "Phiên bản: RV64";
        }
        int ctr = 0;
        private void barItem_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void barButtonItem4_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void barListItem1_ListItemClick(object sender, ListItemClickEventArgs e)
        {

        }

        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
        {
            // Create an MDI child form.
            var f = new Form();
            f.Text = "Child Form " + (++ctr).ToString();
            f.MdiParent = this;
            f.Show();
        }

        private void btnExit_ItemClick(object sender, ItemClickEventArgs e)
        {

            this.Close();

        }

        private void btnImport_ItemClick(object sender, ItemClickEventArgs e)
        {
            //if (FrmImportNew == null || FrmImportNew.IsDisposed)
            //{
            //    FrmImportNew = new NewEditImport(NewEditImport.EditMode.New);
            //    FrmImportNew.OnImportSaved = new Presenters.Import.NewImportPresenter.ImportSaved(onImportSaved);
            //    FrmImportNew.MdiParent = this;
            //    FrmImportNew.Show();
            //}
            ShowImportNewForm();
        }

        public void ShowImportNewForm()
        {
            if (FrmImportNew == null || FrmImportNew.IsDisposed)
            {
                FrmImportNew = new NewEditImport(NewEditImport.EditMode.New);
                FrmImportNew.OnImportSaved = new Presenters.Import.NewImportPresenter.ImportSaved(onImportSaved);
                FrmImportNew.MdiParent = this;
                FrmImportNew.Show();
            }
        }

        public void ShowPONewForm()
        {
            if (FrmNewEditPO == null || FrmNewEditPO.IsDisposed)
            {
                FrmNewEditPO = new NewEditPO(NewEditImport.EditMode.New);
                FrmNewEditPO.OnImportSaved = new WarehouseManagement.Presenters.PO.NewEditPOPresenter.POSaved(onImportSaved);
                FrmNewEditPO.MdiParent = this;
                FrmNewEditPO.Show();
            }
        }

        private void onImportSaved(bool success)
        {
            if (FrmNewEditExport == null || FrmNewEditExport.IsDisposed) return;
            FrmNewEditExport.RefreshDataControls();
        }

        private void btnExport_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowExportNewForm();
        }

        public void ShowExportNewForm()
        {
            if (FrmNewEditExport == null || FrmNewEditExport.IsDisposed)
            {
                FrmNewEditExport = new NewEditExport(NewEditExport.EditMode.New);
                FrmNewEditExport.OnExportSaved = new Presenters.Export.NewEditExportPresenter.ExportSaved(onExportSaved);
                FrmNewEditExport.MdiParent = this;
                FrmNewEditExport.Show();
            }
        }
        private void onExportSaved(bool success)
        {
            if (FrmImportNew == null || FrmImportNew.IsDisposed) return;
            FrmImportNew.RefreshDataControls();
        }

        private void btnViewImport_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (frmViewImport == null || frmViewImport.IsDisposed)
            {
                frmViewImport = new ViewImport();
                frmViewImport.MdiParent = this;
                frmViewImport.Show();
            }
        }

        private void btnViewExport_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (frmViewExport == null || frmViewExport.IsDisposed)
            {
                frmViewExport = new ViewExport();
                frmViewExport.MdiParent = this;
                frmViewExport.Show();
            }
        }

        private void btnViewStockByQuantityAndValue_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (frmStockByQtyAndValue == null || frmStockByQtyAndValue.IsDisposed)
            {
                frmStockByQtyAndValue = new StockByQuantityAndValue();
                frmStockByQtyAndValue.MdiParent = this;
                frmStockByQtyAndValue.Show();
            }
        }

        private void ViewSupplierDebt_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (frmViewSupplierDebt == null || frmViewSupplierDebt.IsDisposed)
            {
                frmViewSupplierDebt = new ViewSupplierDebt();
                frmViewSupplierDebt.MdiParent = this;
                frmViewSupplierDebt.Show();
            }
        }

        private void btnNewPO_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowPONewForm();
        }

        public void ShowViewPOForm()
        {
            if (FrmViewPO == null || FrmViewPO.IsDisposed)
            {
                FrmViewPO = new ViewPO();

                FrmViewPO.MdiParent = this;
                FrmViewPO.Show();
            }
        }

        private void btnViewPOList_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowViewPOForm();
        }

        private void ShowViewReceiptForm()
        {
            if (FrmViewReceipts == null || FrmViewReceipts.IsDisposed)
            {
                FrmViewReceipts = new ViewReceipts();

                FrmViewReceipts.MdiParent = this;
                FrmViewReceipts.Show();
            }
        }
        private void btnViewReceipts_ItemClick(object sender, ItemClickEventArgs e)
        {
            ShowViewReceiptForm();
        }


        private void btnUpdateSupplierDebt_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (FrmUpdateSupplierDebt == null || FrmUpdateSupplierDebt.IsDisposed)
            {
                FrmUpdateSupplierDebt = new UpdateSupplierDebt();
                FrmUpdateSupplierDebt.MdiParent = this;
                FrmUpdateSupplierDebt.Show();
            }
        }

        private void btnInputMerchaniseInStock_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (FrmInputMerchaniseInStock == null || FrmInputMerchaniseInStock.IsDisposed)
            {
                FrmInputMerchaniseInStock = new InputMerchandiseInStock();
                FrmInputMerchaniseInStock.MdiParent = this;
                FrmInputMerchaniseInStock.Show();
            }
        }

        private void btnViewMerchandiseCategory_ItemClick(object sender, ItemClickEventArgs e)
        {

            if (FrmMerchandiseCategoryList == null || FrmMerchandiseCategoryList.IsDisposed)
            {
                FrmMerchandiseCategoryList = new MerchandiseCategoryList();
                FrmMerchandiseCategoryList.MdiParent = this;
                FrmMerchandiseCategoryList.Show();
            }
        }

        private void btnViewRevenue_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (FrmViewRevenue == null || FrmViewRevenue.IsDisposed)
            {
                FrmViewRevenue = new ViewRevenue();
                FrmViewRevenue.MdiParent = this;
                FrmViewRevenue.Show();
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            GlobalMemory.CurrentLoginUser = string.Empty;

            if (OnFormClosed != null)
                OnFormClosed();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (XtraMessageBox.Show("Bạn có thực sự muốn đăng xuất?", "Đăng xuất", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                e.Cancel = true;
        }

        private void btnBackupData_ItemClick(object sender, ItemClickEventArgs e)
        {
            BackupDataForm frmBackup = new BackupDataForm();
            frmBackup.ShowDialog();
        }

        private void btnRestoreData_ItemClick(object sender, ItemClickEventArgs e)
        {
            foreach (var child in this.MdiChildren)
            {
                child.Close();
            }
            RestoreDataForm frmBackup = new RestoreDataForm();
            frmBackup.ShowDialog();
        }

        private void btnChangePassword_ItemClick(object sender, ItemClickEventArgs e)
        {
            var frmChangePassword = new ChangePassword();
            
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            frmChangePassword.ShowDialog();
            SplashScreenManager.CloseForm();

        }

        private void btnMerchandiseList_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (FrmMerchandiseList == null || FrmMerchandiseList.IsDisposed)
            {
                FrmMerchandiseList = new MerchandiseList();
                FrmMerchandiseList.MdiParent = this;
                FrmMerchandiseList.Show();
            }
        }

        private void btnSupplierList_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (FrmSupplierList == null || FrmSupplierList.IsDisposed)
            {
                FrmSupplierList = new SupplierList();
                FrmSupplierList.MdiParent = this;
                FrmSupplierList.Show();
            }
        }

        private void btnCustomerList_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (FrmCustomerList == null || FrmCustomerList.IsDisposed)
            {
                FrmCustomerList = new CustomerList();
                FrmCustomerList.MdiParent = this;
                FrmCustomerList.Show();
            }
        }

        private void btnCurrencyList_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (FrmCurrencyList == null || FrmCurrencyList.IsDisposed)
            {
                FrmCurrencyList = new CurrencyList();
                FrmCurrencyList.MdiParent = this;
                FrmCurrencyList.Show();
            }
        }
    }
}